/*
 * scratch.c
 *
 * Created: 4/11/2013 7:30:59 AM
 *  Author: Parag
 */ 

#ifndef	F_CPU
#define F_CPU 8000000
#endif

#include <avr/io.h>
#include <avr/delay.h>
#include "Motor_Driver.h"
#include "Servo.h"

int i,startUpTime,pumpFrontTime, pumpBackTime,delay_between_start_N_stop;

int main(void)
{
   
	   PORTB |= (1<<PB7) | (1<<PB4);								// Pull up resistors enable Down and Center
	   PORTE |=  ((1<<PE3) | (1<<PE2));								// Right and Left pulled up enable
	   MotorInit();
	   ServoInit();
	   
// START of MOVEMENT TEST CODE----------------------------------XXXXX---------------XXXXXXXXXXXX----------XXXXXXX---------------

	  //MOVEMENT TEST CODE:
	  // IN this code you can only use waterpump and rudder using joysticks ONLY
	  // no main code after you press down
	   
	// place opening braces below this line  anywhere "/*" 
					/*
	
	   while(1)
	   {
		  
			if(bit_is_clear(PINE,PE3))  // Left Button
			{
				MotorOnBack();		
			}				   
			else if(bit_is_clear(PINE,PE2)) //Right Button
			{
				MotorOnForward();
			}				
			else if(bit_is_clear(PINB,PB7)) //Down Button
			{
				ConfigRudderServo(North);
			}				
			else if(bit_is_clear(PINB,PB4)) //Center Button
			{
				ConfigRudderServo(South);
			}				
			else
			{
				MotorOff();
				ConfigRudderServo(West);
			}				
	   }
	   
	  
					 */	
	// place closing comment brackets here  above  this line anywhere"*/"
// END of MOVEMENT TEST CODE----------------------------------XXXXX---------------XXXXXXXXXXXX----------XXXXXXX---------------


	  
//BEGIN TIMER BASED CODE--------------------------------------------------------------------------------------------------------
	//	TIMER BASED PROGRAM:
	//		in the following code, 
	//			Press left or right to adjust the motor
	//			Press down to start the infinite code
	
		// place opening braces below this line  anywhere "/*"


	
		while(bit_is_set(PINB,PB7))				//Down Button
		{
		 	   
			if(bit_is_clear(PINE,PE3))			// Left Button
			{
				MotorOnBack();
			}
			else if(bit_is_clear(PINE,PE2))		//Right Button
			{
				MotorOnForward();
			}
			else
			{
				MotorOff();
				ConfigRudderServo(West);
			}
		} 		
		
// THESE VARIABLES ARE THE ONLY THING THAT MATERRS FOR changing
// VARIABLES to change stuff-------------------------------------------------------------------------------------------------------------
		startUpTime = 40;		// remember these are in {seconds} because it is used in a loop being a minute long each
		pumpFrontTime =	23;
		pumpBackTime =	21;
		delay_between_start_N_stop = 15; // Basically this is how long you want to keep the motor off
//------------------------------------------------IMORTANT VARIABLES above----------------------------------------------------------------
	   
	   // 45 seconds wait before start
	   for(i = 0; i< startUpTime; i++)
	   {
		   _delay_ms(1000);
	   }
	   
	   while(1)
	   {
			MotorOnForward();
			for(i=0; i< pumpFrontTime; i++)
			{
				_delay_ms(1000);
			}
			
			//Now we wait for it to start and actually dive	
			MotorOff();			
						
			// apply delay	   
			for(i=0; i<delay_between_start_N_stop; i++)
			{
				_delay_ms(1000);
			}
		   
			MotorOnBack();
			for(i=0; i< pumpBackTime; i++)
			{
				_delay_ms(1000);
			}
		   
		   //Now we wait for it to start and actually rise	
			MotorOff();	
			
			// apply delay	  
			for(i=0; i< delay_between_start_N_stop; i++)
			{
				_delay_ms(1000);
			}
	   } 
	   
	  	 // place closing comment brackets here  above  this line anywhere"*/"
// END of timing test code-------------------------------------------------------------------------------------------------
	
	
	
} // NEVER comment this bracket out, it closes the first main bracket